package net.tatu.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.tatu.dao.OrgBusinessTargetRelaMapper;
import net.tatu.dao.PubCodeMapper;
import net.tatu.dao.SysLogMapper;
import net.tatu.dao.UserBankCardMapper;
import net.tatu.dao.UserBankCardRelaMapper;
import net.tatu.dao.UserLevelRangeDefineMapper;
import net.tatu.dao.UserMapper;
import net.tatu.dao.UserPointDetailMapper;
import net.tatu.dao.UserPremAccountMapper;
import net.tatu.dao.UserPremDetailMapper;
import net.tatu.model.OrgBusinessTargetRela;
import net.tatu.model.PubCode;
import net.tatu.model.SysLog;
import net.tatu.model.User;
import net.tatu.model.UserBankCard;
import net.tatu.model.UserBankCardRela;
import net.tatu.model.UserLevelRangeDefine;
import net.tatu.model.UserPointDetail;
import net.tatu.model.UserPremAccount;
import net.tatu.model.UserPremDetail;
import net.tatu.service.OrgBusinessTargetRelaService;
import net.tatu.service.PubCodeService;
import net.tatu.service.SysLogService;
import net.tatu.service.UserBankCardService;
import net.tatu.service.UserPointService;
import net.tatu.service.UserPremAccountService;
import net.tatu.service.UserService;
import net.tatu.util.Encrypt;
import net.tatu.util.Util;

@Service
public class UserBankCardServiceImpl implements UserBankCardService {
	Logger log = Logger.getLogger(UserBankCardServiceImpl.class);
	@Autowired
	private UserPremAccountMapper userPremAccountMapper;
	@Autowired
	private UserPremDetailMapper userPremDetailMapper;
	@Autowired
	private UserLevelRangeDefineMapper userLevelRangeDefineMapper;
	@Autowired
	private UserPointDetailMapper userPointDetailMapper;
	@Autowired
	private UserPremAccountService userPremAccountService;
	@Autowired
	private UserBankCardMapper userBankCardMapper;
	@Autowired
	private UserBankCardRelaMapper userBankCardRelaMapper;
	@Autowired
	private UserService userService;

	@Override
	public List<UserBankCard> getCards(User u) {
		List<UserBankCard> cards = new ArrayList<UserBankCard>();
		UserBankCardRela userBankCardRela = new UserBankCardRela();
		userBankCardRela.setUserPremActId(u.getUserId());
		List<UserBankCardRela> cardRelas = userBankCardRelaMapper.getUserBankCardRelas(userBankCardRela);
		UserBankCard userBankCard = new UserBankCard();
		for(int i = 0; i < cardRelas.size(); i++){
			UserBankCardRela ubcr = cardRelas.get(i);
			userBankCard.setBankCardNo(ubcr.getBankCardNo());
			List<UserBankCard> list = userBankCardMapper.getUserBankCards(userBankCard);
			if(list.size() > 0){
				log.debug("读取用户卡片时增加了list：大小：" + list.size());
				cards.addAll(list);
				log.debug("读取用户卡片时增加了list：大小：" + cards.size());
			}
		}
		if(cards.size() < 0){
			return null;
		}
		return cards;
	}

	@Override
	public boolean addBankCard(HttpServletRequest request,
			UserBankCard userBankCard) {
		boolean ret = false;
		User u = (User) request.getSession().getAttribute("user");
		UserBankCardRela userBankCardRela = new UserBankCardRela();
		userBankCardRela.setUserPremActId(u.getUserId());
		userBankCardRela.setBankCardNo(userBankCard.getBankCardNo());
		userBankCardRela.setRecordStatusCode(1);
		int b1 = userBankCardRelaMapper.insertUserBankCardRela(userBankCardRela);
		userBankCard.setRecordStatusCode(1);
		int b2 = userBankCardMapper.insertUserBankCard(userBankCard);
		if(b1 == 1 && b2 == 1){
			ret = true;
		}else{
			log.error("用户添加银行卡失败！userId：" + u.getUserId());
		}
		return ret;
	}
}
